package cn.maszy.service.impl.projectmanagement;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineerCategoryMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineeringMapper;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategory;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategoryExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategoryExample.Criteria;
import cn.maszy.model.projectmanagement.B_T_Ufp_Engineering;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineerCategoryService;
@Service
public class B_T_Ufp_EngineerCategoryServiceImpl implements B_T_Ufp_EngineerCategoryService {
	@Autowired
	private B_T_Ufp_EngineerCategoryMapper b_T_Ufp_EngineerCategoryMapper;
	@Autowired
	private B_T_Ufp_EngineeringMapper b_T_Ufp_EngineeringMapper;
	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#update(cn.maszy.model.investment.B_T_Ufp_EngineerCategory, cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int update(B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory) {
		// TODO Auto-generated method stub
		 if(b_T_Ufp_EngineerCategory.getUuid()==null||"".equals(b_T_Ufp_EngineerCategory.getUuid())){
			 //查询是否有相同的项目名
			 B_T_Ufp_EngineerCategoryExample example=new B_T_Ufp_EngineerCategoryExample();
			 Criteria criteria = example.createCriteria();
				if(b_T_Ufp_EngineerCategory!=null){
					if(b_T_Ufp_EngineerCategory.getProjectNum()!=null&&!"".equals(b_T_Ufp_EngineerCategory.getProjectNum())){
						criteria.andProjectNumEqualTo(b_T_Ufp_EngineerCategory.getProjectNum());
					}
					if(b_T_Ufp_EngineerCategory.getPositionName()!=null&&!"".equals(b_T_Ufp_EngineerCategory.getPositionName())){
						criteria.andPositionNameEqualTo(b_T_Ufp_EngineerCategory.getPositionName());
					}
					if(b_T_Ufp_EngineerCategory.getPositionUuid()!=null&&!"".equals(b_T_Ufp_EngineerCategory.getPositionUuid())){
						criteria.andPositionUuidEqualTo(b_T_Ufp_EngineerCategory.getPositionUuid());
					}
				}else{
						criteria.andUuidIsNotNull();
				}
				example.or(criteria);
				example.setOrderByClause("create_time");
				// 取得总的记录条数
				List<B_T_Ufp_EngineerCategory>  list=b_T_Ufp_EngineerCategoryMapper.selectByExample(example);
				 if(list.size()>=1)
				 {
					 //发现有相同的话，就把查出的uuid设置给新来的数据。并执行修改
					 b_T_Ufp_EngineerCategory.setUuid(list.get(0).getUuid());
					 //将2条重复数据的数量和总金额相加
					 float j =b_T_Ufp_EngineerCategory.getQuantities();
					 float k =list.get(0).getQuantities();
					 b_T_Ufp_EngineerCategory.setQuantities(j+k);
					 float totalAmount = list.get(0).getTotalAmount();
					 float totalAmount1= b_T_Ufp_EngineerCategory.getTotalAmount();
					 b_T_Ufp_EngineerCategory.setTotalAmount(totalAmount+totalAmount1);
					 //执行修改操作
					 b_T_Ufp_EngineerCategory.setUpdateTime(new Date());
					 b_T_Ufp_EngineerCategory.setUpdateUser(SessionFactory.getSessionUserRealNam());
					 return b_T_Ufp_EngineerCategoryMapper.updateByPrimaryKeySelective(b_T_Ufp_EngineerCategory);
				 }
			 
			 
			 b_T_Ufp_EngineerCategory.setUuid(UUID.randomUUID().toString());
			 b_T_Ufp_EngineerCategory.setIsDelete(0);
			 b_T_Ufp_EngineerCategory.setCreateTime(new Date());
			 b_T_Ufp_EngineerCategory.setCreateUser(SessionFactory.getSessionUserRealNam());
			 return b_T_Ufp_EngineerCategoryMapper.insert(b_T_Ufp_EngineerCategory);
		 }else{
			 b_T_Ufp_EngineerCategory.setUpdateTime(new Date());
			 b_T_Ufp_EngineerCategory.setUpdateUser(SessionFactory.getSessionUserRealNam());
			 return b_T_Ufp_EngineerCategoryMapper.updateByPrimaryKeySelective(b_T_Ufp_EngineerCategory);
		 }
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#selectOne(java.lang.String)
	 */
	public B_T_Ufp_EngineerCategory selectOne(String uuid) {
		// TODO Auto-generated method stub
		B_T_Ufp_EngineerCategory record=b_T_Ufp_EngineerCategoryMapper.selectByPrimaryKey(uuid);
		return record;
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#delete(java.lang.String)
	 */
	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		for (String id : checkIds.split(", ")) {
			if(!"on".equals(id))
			{
				B_T_Ufp_EngineerCategory record=b_T_Ufp_EngineerCategoryMapper.selectByPrimaryKey(id);
				b_T_Ufp_EngineerCategoryMapper.deleteByPrimaryKey(id);
				//同时修改项目总价格
				//根据uuid 查询到所属的工程id
				float price =totalall(record);
				B_T_Ufp_Engineering b_T_Ufp_Engineering =new B_T_Ufp_Engineering();
				b_T_Ufp_Engineering.setUuid(record.getEngineeringUuid());
				b_T_Ufp_Engineering.setEngineeringPrice(price);
				b_T_Ufp_EngineeringMapper.updateByPrimaryKeySelective(b_T_Ufp_Engineering);
			}
			
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#count(cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int count(B_T_Ufp_EngineerCategoryExample example,B_T_Ufp_EngineerCategoryMapper b_T_Ufp_EngineerCategoryMapper) {
		// TODO Auto-generated method stub
		return b_T_Ufp_EngineerCategoryMapper.countByExample(example);
	}

	/* (non-Javadoc)
	 * @see cn.maszy.service.investment.B_T_Ufp_AdminService#selectList(cn.maszy.model.investment.B_T_Ufp_EngineerCategory)
	 */
	public Pager<B_T_Ufp_EngineerCategory> selectList(B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory,Pager<B_T_Ufp_EngineerCategory> pager) {
		// TODO Auto-generated method stub
		// 添加查询条件
		B_T_Ufp_EngineerCategoryExample example=new B_T_Ufp_EngineerCategoryExample();
		Criteria criteria = example.createCriteria();
		if(b_T_Ufp_EngineerCategory!=null){
			if(b_T_Ufp_EngineerCategory.getPositionUuid()!=null&&!"".equals(b_T_Ufp_EngineerCategory.getPositionUuid())){
				criteria.andPositionUuidEqualTo(b_T_Ufp_EngineerCategory.getPositionUuid());
			}
//			if(b_T_Ufp_EngineerCategory.getCategoryName()!=null&&!"".equals(b_T_Ufp_EngineerCategory.getCategoryName())){
//				criteria.andCategoryNameEqualTo(b_T_Ufp_EngineerCategory.getCategoryName());
//			}
//			if(b_T_Ufp_EngineerCategory.getProjectName()!=null&&!"".equals(b_T_Ufp_EngineerCategory.getProjectName())){
//				criteria.andProjectNameLike("%"+b_T_Ufp_EngineerCategory.getProjectName()+"%");
//			}
			
		}else{
			criteria.andUuidIsNotNull();
		}
		example.or(criteria);
//		int totalCount=new B_T_Ufp_EngineerCategoryServiceImpl().count(example,b_T_Ufp_EngineerCategoryMapper);
//		pager.setTotalRecords(totalCount);
//		example.setLimitStart(pager.getStartRecord());
//		example.setLimitEnd(pager.getPageSize());
		example.setOrderByClause("create_time");
		// 取得总的记录条数
		List<B_T_Ufp_EngineerCategory>  list=b_T_Ufp_EngineerCategoryMapper.selectByExample(example);
		pager.setPageRecords(list);
		return pager;
	}
	//根据部位id计算总金额
	public float totalamount(B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory)
	{
		B_T_Ufp_EngineerCategoryExample example=new B_T_Ufp_EngineerCategoryExample();
		String sql = "SELECT SUM(total_amount)AS total_amount FROM b_t_ufp_engineercategory WHERE  position_uuid ='"+b_T_Ufp_EngineerCategory.getPositionUuid()+"'";
		example.setOrderByClause(sql);
		List<B_T_Ufp_EngineerCategory>  list=b_T_Ufp_EngineerCategoryMapper.selectByExampleby(example);
		Float i=(float) 0;
		
		if(list.get(0) == null)
		{
			return 0;
		}
		else
		{
			i = list.get(0).getTotalAmount();
		}
		return i;
	}
	//根据工程id计算总金额
	public float totalall(B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory)
	{
		B_T_Ufp_EngineerCategoryExample example=new B_T_Ufp_EngineerCategoryExample();
		String sql = "SELECT SUM(total_amount)AS total_amount FROM b_t_ufp_engineercategory WHERE  engineering_uuid ='"+b_T_Ufp_EngineerCategory.getEngineeringUuid()+"'";
		example.setOrderByClause(sql);
		List<B_T_Ufp_EngineerCategory>  list=b_T_Ufp_EngineerCategoryMapper.selectByExampleby(example);
		Float i=(float) 0;
		if(list.get(0) == null)
		{
			return 0;
		}
		else
		{
			i = list.get(0).getTotalAmount();
		}
		return i;
	}
	
	public List<B_T_Ufp_EngineerCategory> select(B_T_Ufp_EngineerCategory b_T_Ufp_EngineerCategory) {
		// TODO Auto-generated method stub
		// 添加查询条件
		B_T_Ufp_EngineerCategoryExample example=new B_T_Ufp_EngineerCategoryExample();
		Criteria criteria = example.createCriteria();
		if(b_T_Ufp_EngineerCategory!=null){
			if(b_T_Ufp_EngineerCategory.getPositionUuid()!=null&&!"".equals(b_T_Ufp_EngineerCategory.getPositionUuid())){
				criteria.andPositionUuidEqualTo(b_T_Ufp_EngineerCategory.getPositionUuid());
			}
			
		}else{
			criteria.andUuidIsNotNull();
		}
		example.or(criteria);
		example.setOrderByClause("create_time");
		// 取得总的记录条数
		List<B_T_Ufp_EngineerCategory>  list=b_T_Ufp_EngineerCategoryMapper.selectByExample(example);
		return list;
	}
	
}
